<%@ page import="com.wl.shixun1.mymvc.DoNum" %>
<%@ page import="com.wl.shixun1.mymvc.Num" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>2350300097 万朗</title>
</head>
<body>
<%
    String x1 = request.getParameter("x1");
    String y1 = request.getParameter("y1");
    String x2 = request.getParameter("x2");
    String y2 = request.getParameter("y2");
    String x3 = request.getParameter("x3");
    String y3 = request.getParameter("y3");
    String x4 = request.getParameter("x4");
    String y4 = request.getParameter("y4");

    String op1 = request.getParameter("op1");
    String op2 = request.getParameter("op2");
    String op3 = request.getParameter("op3");
    String op4 = request.getParameter("op4");

    String z1 = "";
    String z2 = "";
    String z3 = "";
    String z4 = "";

    String expr1 = "";
    String expr2 = "";
    String expr3 = "";
    String expr4 = "";

    // 检查是否为空字符串，而不仅仅是null
    if (x1 != null && !x1.trim().isEmpty() && y1 != null && !y1.trim().isEmpty()) {

        try {
            Num xx = DoNum.fun(x1);
            Num yy = DoNum.fun(y1);
            Num zz;

            switch(op1) {
                case "sub":
                    zz = xx.sub(yy);
                    expr1 = x1 + " - " + y1 + " = ";
                    break;
                case "mul":
                    zz = xx.mul(yy);
                    expr1 = x1 + " × " + y1 + " = ";
                    break;
                case "div":
                    zz = xx.div(yy);
                    expr1 = x1 + " ÷ " + y1 + " = ";
                    break;
                case "add":
                default:
                    zz = xx.add(yy);
                    expr1 = x1 + " + " + y1 + " = ";
                    break;
            }

            z1 = zz.draw1();
            expr1 += z1; // 将结果添加到表达式中
        } catch (Exception e) {
            z1 = "错误";
            expr1 = "计算错误";
        }
    }

    if (x2 != null && !x2.trim().isEmpty() && y2 != null && !y2.trim().isEmpty()) {
        try {
            Num xx = DoNum.fun(x2);
            Num yy = DoNum.fun(y2);
            Num zz;

            switch(op2) {
                case "sub":
                    zz = xx.sub(yy);
                    expr2 = x2 + " - " + y2 + " = ";
                    break;
                case "mul":
                    zz = xx.mul(yy);
                    expr2 = x2 + " × " + y2 + " = ";
                    break;
                case "div":
                    zz = xx.div(yy);
                    expr2 = x2 + " ÷ " + y2 + " = ";
                    break;
                case "add":
                default:
                    zz = xx.add(yy);
                    expr2 = x2 + " + " + y2 + " = ";
                    break;
            }

            z2 = zz.draw1();
            expr2 += z2;
        } catch (Exception e) {
            z2 = "错误";
            expr2 = "计算错误";
        }
    }
    if (x3 != null && !x3.trim().isEmpty() && y3 != null && !y3.trim().isEmpty()) {
        try {
            Num xx = DoNum.fun(x3);
            Num yy = DoNum.fun(y3);
            Num zz;

            switch(op3) {
                case "add":
                    zz = xx.add(yy);
                    expr3 = x3 + " + " + y3 + " = ";
                    break;
                case "sub":
                    zz = xx.sub(yy);
                    expr3 = x3 + " - " + y3 + " = ";
                    break;
                case "mul":
                    zz = xx.mul(yy);
                    expr3 = x3 + " × " + y3 + " = ";
                    break;
                case "div":
                    zz = xx.div(yy);
                    expr3 = x3 + " ÷ " + y3 + " = ";
                    break;
                default:
                    zz = xx.add(yy);
                    expr3 = x3 + " + " + y3 + " = ";
                    break;
            }

            z3 = zz.draw1();
            expr3 += z3;
        } catch (Exception e) {
            z3 = "错误";
            expr3 = "计算错误";
        }
    }
    if (x4 != null && !x4.trim().isEmpty() && y4 != null && !y4.trim().isEmpty()) {
        try {
            Num xx = DoNum.fun(x4);
            Num yy = DoNum.fun(y4);
            Num zz;

            switch(op4) {
                case "add":
                    zz = xx.add(yy);
                    expr4 = x4 + " + " + y4 + " = ";
                    break;
                case "sub":
                    zz = xx.sub(yy);
                    expr4 = x4 + " - " + y4 + " = ";
                    break;
                case "mul":
                    zz = xx.mul(yy);
                    expr4 = x4 + " × " + y4 + " = ";
                    break;
                case "div":
                    zz = xx.div(yy);
                    expr4 = x4 + " ÷ " + y4 + " = ";
                    break;
                default:
                    zz = xx.add(yy);
                    expr4 = x4 + " + " + y4 + " = ";
                    break;
            }

            z4 = zz.draw1();
            expr4 += z4;
        } catch (Exception e) {
            z4 = "错误";
            expr4 = "计算错误";
        }
    }
%>

<form action="" method="post">


    <input type="text" name="x1" id="x1" value="<%=x1 == null ? "" : x1%>" >
    <select name="op1" id="op1">
        <option value="add" <%= "add".equals(op1) ? "selected" : "" %>>+ 加</option>
        <option value="sub" <%= "sub".equals(op1) ? "selected" : "" %>>- 减</option>
        <option value="mul" <%= "mul".equals(op1) ? "selected" : "" %>>* 乘</option>
        <option value="div" <%= "div".equals(op1) ? "selected" : "" %>>/ 除</option>
    </select>
    <input type="text" name="y1" id="y1" value="<%=y1 == null ? "" : y1%>" >
    <input type="submit" name="button1" value="=">
    <input type="text" class="result" value="<%=z1%>" readonly>
    <span><%=expr1%></span>



    <!-- 第二行 -->
    <div>
        <input type="text" name="x2" id="x2" value="<%=x2 == null ? "" : x2%>" >
        <select name="op2" id="op2">
            <option value="sub" <%= "sub".equals(op2) ? "selected" : "" %>>- 减</option>
            <option value="add" <%= "add".equals(op2) ? "selected" : "" %>>+ 加</option>
            <option value="mul" <%= "mul".equals(op2) ? "selected" : "" %>>* 乘</option>
            <option value="div" <%= "div".equals(op2) ? "selected" : "" %>>/ 除</option>
        </select>
        <input type="text" name="y2" id="y2" value="<%=y2 == null ? "" : y2%>" >
        <input type="submit" name="button2" value="=">
        <input type="text" class="result" value="<%=z2%>" readonly>
        <span><%=expr2%></span>
    </div>

    <!-- 第三行 -->
    <div>
        <input type="text" name="x3" id="x3" value="<%=x3 == null ? "" : x3%>" >
        <select name="op3" id="op3">
            <option value="mul" <%= "mul".equals(op3) ? "selected" : "" %>>* 乘</option>
            <option value="add" <%= "add".equals(op3) ? "selected" : "" %>>+ 加</option>
            <option value="sub" <%= "sub".equals(op3) ? "selected" : "" %>>- 减</option>
            <option value="div" <%= "div".equals(op3) ? "selected" : "" %>>/ 除</option>
        </select>
        <input type="text" name="y3" id="y3" value="<%=y3 == null ? "" : y3%>" >
        <input type="submit" name="button3" value="=">
        <input type="text" class="result" value="<%=z3%>" readonly>
        <span><%=expr3%></span>
    </div>

    <!-- 第四行 -->
    <div>
        <input type="text" name="x4" id="x4" value="<%=x4 == null ? "" : x4%>" >
        <select name="op4" id="op4">
            <option value="div" <%= "div".equals(op4) ? "selected" : "" %>>/ 除</option>
            <option value="add" <%= "add".equals(op4) ? "selected" : "" %>>+ 加</option>
            <option value="sub" <%= "sub".equals(op4) ? "selected" : "" %>>- 减</option>
            <option value="mul" <%= "mul".equals(op4) ? "selected" : "" %>>* 乘</option>
        </select>
        <input type="text" name="y4" id="y4" value="<%=y4 == null ? "" : y4%>" >
        <input type="submit" name="button4" value="=">
        <input type="text" class="result" value="<%=z4%>" readonly>
        <span><%=expr4%></span>
    </div>
</form>

</body>
</html>